package com.rjwh.dingdong.client.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aishua.lib.utils.AsStrUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.rjwh.dingdong.client.R;
import com.rjwh.dingdong.client.activity.message.MessageListActivity;
import com.rjwh.dingdong.client.bean.localbean.MessageListItemData;
import com.rjwh.dingdong.client.util.UrlUtil;
import com.rjwh.dingdong.client.util.ViewHolder;

/**
 * @author Kay
 * @ClassName MessageListAdapter
 * @Description
 * @date 2016-12-27
 * @history 2016-12-27 author: description:
 */

public class MessageListAdapter extends AdapterBase<MessageListItemData> {

    private Context context;

    public MessageListAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected View getExView(int position, View convertView, ViewGroup parent) throws Exception {
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.message_list_item_layout, null);
        }
        LinearLayout rootLL = ViewHolder.get(convertView, R.id.message_list_item_root);
        ImageView img = ViewHolder.get(convertView, R.id.message_list_item_image);
        TextView titleTv = ViewHolder.get(convertView, R.id.message_list_item_title);
        TextView descTv = ViewHolder.get(convertView, R.id.message_list_item_subtitle);
        TextView timeLineTv = ViewHolder.get(convertView, R.id.message_list_item_time_line);
        TextView timeTv = ViewHolder.get(convertView, R.id.message_list_item_time);

        MessageListItemData messageListItemData = getList().get(position);
        if (messageListItemData == null) {
            return convertView;
        }

        String image = messageListItemData.getImage();
        String title = messageListItemData.getTitle();
        String subtitle = messageListItemData.getSubtitle();
        String time = messageListItemData.getTime();
        String timeline = messageListItemData.getTimeline();
        final String actionurl = messageListItemData.getActionurl();

        Glide.with(context).load(image).dontAnimate().placeholder(R.drawable.icon_pic_holder).error(R.drawable.icon_pic_holder)
                .diskCacheStrategy(DiskCacheStrategy.ALL).into(img);

        if (!AsStrUtils.isEmpty(title)) {
            titleTv.setText(title);
        }
        if (!AsStrUtils.isEmpty(subtitle)) {
            descTv.setText(subtitle);
        }
        if (!AsStrUtils.isEmpty(timeline)) {
            timeLineTv.setText(timeline);
        }
        if (!AsStrUtils.isEmpty(time)) {
            timeTv.setText(time);
        }

        rootLL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                UrlUtil.handelUrl(context,actionurl,true);
            }
        });

        return convertView;
    }
}
